package com.zwy.booksystem.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zwy.booksystem.pojo.Book;
import com.zwy.booksystem.service.impl.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxpdmw
 * @since 2020-05-02
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookServiceImpl bookService;
    
    @RequestMapping("/allBook")
    public String select(Model model){
        List<Book> list = bookService.list(null);
        model.addAttribute(list);
        return "allBook";
    }

    @RequestMapping("/toUpdate/{bookID}")
    public String toUpdate(@PathVariable("bookID") Integer id, Model model){
        Book book = bookService.getById(id);
        model.addAttribute(book);
        return "updateBook";

    }

    @RequestMapping("/update")
    public String update(Book book){
        bookService.updateById(book);
        return "redirect:/book/allBook";

    }

    @RequestMapping("/delete/{bookID}")
    public String delete(@PathVariable("bookID")Integer id){
        bookService.removeById(id);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "addBook";
    }

    @RequestMapping("/add")
    public String addBook(Book book){
        bookService.save(book);
        return "redirect:/book/allBook";

    }



}



